﻿using AutoMapper;
using AutoMapper.Internal;
using CSRedis;
using EMS.system.Common;
using EMS.system.Entityies.MasterData.MaterialTypeModel;
using EMS.system.IEntityAppService.MasterData.MaterialTypeIService;
using EMS.system.IEntityAppService.MasterData.MaterialTypeIService.MaterialTypeDTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace EMS.system.EntityAppService.MasterData.MaterialTypeService
{
    /// <summary>
    /// 物料类型服务实现
    /// </summary>
    public class MaterialTypeAppService : ApplicationService, IMaterialTypeAppService
    {
        private readonly IRepository<MaterialType, int> _repository;
        private static Object obj = new Object();

        public MaterialTypeAppService(IRepository<MaterialType, int> repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 物料类型新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> MaterialTypeInsert(MaterialTypeInsertDTO model)
        {
            var typeModel = ObjectMapper.Map<MaterialTypeInsertDTO, MaterialType>(model);
            await _repository.InsertAsync(typeModel);
            return 1;
        }


        /// <summary>
        /// 调用递归获取数据
        /// </summary>
        /// <param name="materialTypeName"></param>
        /// <returns></returns>
        public async Task<List<MaterialTypeSelectDTO>> GetMaterialType()
        {
            var materialTypeList = await _repository.GetListAsync();
            var materialTypes = await GetMaterialType(materialTypeList,0);
            return materialTypes;
        }

        /// <summary>
        /// 递归
        /// </summary>
        /// <param name="materialTypeList"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<MaterialTypeSelectDTO>> GetMaterialType(List<MaterialType> materialTypeList, int id=0)
        {
            var list = materialTypeList.Where(x => x.ParentId == id);
            List<MaterialTypeSelectDTO> seldtoList = new List<MaterialTypeSelectDTO>();
            foreach (var item in list)
            {
                MaterialTypeSelectDTO selectDTO = item.MapTo<MaterialTypeSelectDTO>();
                selectDTO.Children = GetMaterialType(materialTypeList,item.Id).Result;
                seldtoList.Add(selectDTO);
            }
            return seldtoList;
        }

        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<MaterialType>> GetAll()
        {
            var typeList = EMS.system.Common.RedisHelper.Get<List<MaterialType>>("typeListKey");
            if (typeList == null)
            {
                lock (obj)
                {
                    if (typeList == null)
                    {
                        typeList = _repository.GetListAsync().Result;
                        var seconds = 30000 + new Random().Next(1, 1000);
                        EMS.system.Common.RedisHelper.Set("typeListKey", typeList,DateTime.Now.AddDays(1));
                    }
                }
            }
            return typeList;
        }

        /// <summary>
        /// 物料类型删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<int> MaterialTypeDelete(int id)
        {
            await _repository.DeleteAsync(id);
            return 1;
        }

        /// <summary>
        /// 获取单个物料类型
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<MaterialType> GetSingMaterialType(int id)
        {
            return await _repository.FindAsync(x => x.Id == id);
        }

        /// <summary>
        /// 物料类型编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> MaterialTypeUpdate(MaterialTypeUpdateDTO model)
        {
            var typeModel = ObjectMapper.Map<MaterialTypeUpdateDTO, MaterialType>(model);
            await _repository.UpdateAsync(typeModel);
            return 1;
        }
    }
}
